package com.yuandian.business.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.entity.BusinessStepLearningEntity;
import com.yuandian.api.business_monitor.entity.BusinessStepLearningParams;
import com.yuandian.business.monitor.mapper.BusinessStepLearningMapper;
import com.yuandian.business.monitor.service.BusinessStepLearningService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.StringUtils;
import com.yuandian.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lijunhang
 * @date 2023年07月13日 9:13
 * @description
 */
@Service
public class BusinessStepLearningServiceImpl implements BusinessStepLearningService {

    @Autowired
    private BusinessStepLearningMapper mapper;

//    @DS("bpmData")
    @Override
    public PageDataInfo selectBusiStepLearningList(BusinessStepLearningParams params) {
        Page<BusinessStepLearningEntity> page = new Page<>(params.getPageNum(), params.getPageSize());
        List<BusinessStepLearningEntity> list = mapper.selectBusinessStepLearningList(page, params);
        return new PageDataInfo(page, list);
    }

//    @DS("bpmData")
    @Override
    public void downloadBusiStepLearningList(BusinessStepLearningParams params, HttpServletResponse response) throws IOException {

        List<BusinessStepLearningEntity> list = mapper.downloadBusiStepLearningList(params);
        for (BusinessStepLearningEntity businessStepLearningEntity : list) {
            if (StringUtils.isNotBlank(businessStepLearningEntity.getRequestMsgDetail())&&businessStepLearningEntity.getRequestMsgDetail().length()>30000){
                businessStepLearningEntity.setRequestMsgDetail(businessStepLearningEntity.getRequestMsgDetail().substring(0,30000));
            }
            if (StringUtils.isNotBlank(businessStepLearningEntity.getResponseMsgDetail())&&businessStepLearningEntity.getResponseMsgDetail().length()>30000){
                businessStepLearningEntity.setResponseMsgDetail(businessStepLearningEntity.getResponseMsgDetail().substring(0,30000));
            }

            if (StringUtils.isNotBlank(businessStepLearningEntity.getDecRequestMsgDetail())&&
                    businessStepLearningEntity.getDecRequestMsgDetail().length()>30000){
                businessStepLearningEntity.setDecRequestMsgDetail(businessStepLearningEntity.getDecRequestMsgDetail().substring(0,30000));
            }
            if (StringUtils.isNotBlank(businessStepLearningEntity.getDecResponseMsgDetail())&&businessStepLearningEntity.getDecResponseMsgDetail().length()>30000){
                businessStepLearningEntity.setDecResponseMsgDetail(businessStepLearningEntity.getDecResponseMsgDetail().substring(0,30000));
            }
        }
        String formTitleName = "业务步骤串联结果数据";
        List<List<String>> headList = new ArrayList<>();
        List<String> titleList = Arrays.asList("批次时间", "业务编码", "业务名称", "系统编码", "系统名称", "首步骤业务服务资源编码",
                "业务序号", "步骤序号", "业务服务资源编码","sessionId", "执行次数",
                "请求详细报文", "响应详细报文", "解码解密后的请求详细报文", "解码解密后的响应详情报文"
                );
        titleList.forEach(s -> {
            List<String> childHeadList = new ArrayList<>();
            childHeadList.add(formTitleName);
            childHeadList.add(s);
            headList.add(childHeadList);
        });
        ExcelUtil.writeExcel(BusinessStepLearningEntity.class, formTitleName, "原始数据样本列表", list, headList, response);

    }
}
